/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import AdminOperations.AdminManageTheme;
import beans.ThemesFormBean;
import beans.UserType;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;
import xmlMapping.Theme;

/**
 *
 * @author ABDELMOHSEN
 */
public class GetThemesAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserType type=(UserType)request.getSession().getAttribute("type");
        if(type==null||!type.equals(UserType.adm)) return mapping.findForward("login");
        AdminManageTheme model=new AdminManageTheme();
        ThemesFormBean bean=(ThemesFormBean)form;
        Iterator<Theme> themes=model.getAllThemes().iterator();
        ArrayList<LabelValueBean> list=new ArrayList<LabelValueBean>();
        int i=0;
        while(themes.hasNext()){
            list.add(new LabelValueBean(themes.next().getName(), String.valueOf(i)));
            i++;
        }
        bean.setThemeList(list);
        return mapping.findForward("displayTh");
    }
}
